#include "board.h"
#include "drv_lcd.h"
#include "lcd_port.h"
#include <rtdevice.h>
#include <string.h>

static rt_device_t device_lcd;
static struct rt_device_graphic_info lcd_info;
static struct rt_device_rect_info rect_info;

void board_lcd_flush(uint16_t x_start, uint16_t y_start, uint16_t x_len, uint16_t y_len, lcd_color_t *color_p)
{
    lcd_color_t* lcd_buff; 
    lcd_color_t* src_buff;
    uint16_t x_index = 0,y_index = 0;
    if((x_start+x_len)>lcd_info.width)return;
    if((y_start+y_len)>lcd_info.height)return;

    if( (lcd_info.bits_per_pixel == 24) || (lcd_info.bits_per_pixel == 32))
    {
        src_buff = color_p;
        y_index = 0;
        do{
            lcd_buff = (lcd_color_t *)lcd_info.framebuffer;
            lcd_buff = lcd_buff + ( lcd_info.width * (y_start+y_index))+ x_start;
            x_index = 0;
            do{
                *lcd_buff = *src_buff;
                src_buff++;
                lcd_buff++;
                x_index++;
            }while(x_index < x_len);
            y_index++;
        }while(y_index < y_len);
    }
    rect_info.x = x_start;
    rect_info.y = y_start;
    rect_info.width = x_len;
    rect_info.height = y_len;
    rt_device_control(device_lcd, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
}

void board_lcd_fill_pic(uint16_t x_start, uint16_t y_start, uint16_t x_len, uint16_t y_len, lcd_color_t *pic_buff)
{
    board_lcd_flush(x_start,y_start,x_len,y_len,pic_buff);
}

void board_lcd_pixel(uint16_t x,uint16_t y, lcd_color_t color)
{
    if( (lcd_info.bits_per_pixel == 24) || (lcd_info.bits_per_pixel == 32))
    {
	    lcd_color_t *lcd_buff = (lcd_color_t *)lcd_info.framebuffer;

        lcd_buff[ (y * lcd_info.width) + x] = color;
    }
}

void board_lcd_box(uint16_t x_start, uint16_t y_start, uint16_t x_len, uint16_t y_len, lcd_color_t color)
{
    lcd_color_t* lcd_buff; 
    uint16_t x_index = 0,y_index = 0;
    if((x_start+x_len)>lcd_info.width)return;
    if((y_start+y_len)>lcd_info.height)return;
    
    if( (lcd_info.bits_per_pixel == 24) || (lcd_info.bits_per_pixel == 32))
    {
        y_index = 0;
        do{
            lcd_buff = (lcd_color_t *)lcd_info.framebuffer;
            lcd_buff = lcd_buff + ( lcd_info.width * (y_start+y_index))+ x_start;
            x_index = 0;
            do{
                *lcd_buff = color;
                lcd_buff++;
                x_index++;
            }while(x_index < x_len);
            y_index++;
        }while(y_index < y_len);
    }
    rect_info.x = x_start;
    rect_info.y = y_start;
    rect_info.width = x_len;
    rect_info.height = y_len;
    rt_device_control(device_lcd, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
}

static int _lcd_init = 0;

int drv_lcd_init(void)
{
    if(_lcd_init == 1)
        return 0;

    _lcd_init =  1;
	rt_kprintf("%s\n",__FUNCTION__);
    
    /* LCD Device Init */
    device_lcd = rt_device_find("lcd");
    RT_ASSERT(device_lcd != RT_NULL);

    rt_device_open(device_lcd, RT_DEVICE_OFLAG_RDWR);

    rt_device_control(device_lcd, RTGRAPHIC_CTRL_GET_INFO, &lcd_info);
    rt_kprintf("lcd pixel       :%d\n", lcd_info.bits_per_pixel);
    rt_kprintf("lcd height      :%d\n", lcd_info.height);
    rt_kprintf("lcd width       :%d\n", lcd_info.width);
    rt_kprintf("lcd framebuffer :%p\n", lcd_info.framebuffer);
	return 0;
}



